// NewBonusWindow.xaml.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Windows;

using Supremacy.Economy;
using Supremacy.Types;

namespace Supremacy.Editor
{
    /// <summary>
    /// Interaction logic for NewBonusWindow.xaml
    /// </summary>

    public partial class NewBonusWindow
    {
        public NewBonusWindow()
        {
            InitializeComponent();

            foreach (BonusType bonusType in Enum.GetValues(typeof(BonusType)))
            {
                BonusTypeList.Items.Add(bonusType);
            }

            if (BonusTypeList.Items.Count > 0)
            {
                BonusTypeList.SelectedIndex = 0;
            }

            Loaded += NewBonusWindow_Loaded;

            AcceptButton.Click += AcceptButton_Click;
        }

        void NewBonusWindow_Loaded(object sender, RoutedEventArgs e)
        {
            AmountText.Focus();
        }

        void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            int amount;
            if (int.TryParse(AmountText.Text, out amount))
            {
                DialogResult = true;
            }
            else
            {
                MessageBox.Show(
                    "You must enter an amount greater than zero.",
                    "Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
            }
        }

        public static Bonus? GetSelection()
        {
            NewBonusWindow dialog = new NewBonusWindow();
            dialog.Owner = EditorApp.Current.MainWindow;
            dialog.ShowDialog();
            if (dialog.DialogResult.HasValue)
            {
                try
                {
                    return new Bonus(
                        (BonusType)dialog.BonusTypeList.SelectedItem,
                        Number.ParseInt32(dialog.AmountText.Text));
                }
                catch { }
            }
            return null;
        }

    }
}